import { defineStore } from "pinia";
import { getPersonInfo, getDataset, getFeatures, getOpenDataset } from "@/api";
import WKT from "terraformer-wkt-parser";
import { IDataset, ILayer } from "@/struct";
export const useMapStore = defineStore("map", {
  state: () => {
    return {
      datasetInfo: {
        id: 1,
        createTime: "2023-12-19 19:46:53",
        updateTime: "2023-12-19 19:46:53",
        pid: "3cc18de0-9e64-11ee-b7f5-07df68be1add",
        name: "默认数据集",
        img: "https://rcs-bucket.oss-cn-hangzhou.aliyuncs.com/app%2Fspace%2F4b2eac181cfb4c6a80c3110cf77e82f1_img-2.png",
        location: "POINT (116.32 39.89)",
        scale: 9,
        status: 1,
        style: "",
        layers: [
          {
            id: 1,
            createTime: "2023-12-19 19:52:03",
            updateTime: "2024-01-04 09:41:38",
            name: "",
            pid: "f4830710-9e64-11ee-adb5-3b52745da4bb",
            img: "https://rcs-bucket.oss-cn-hangzhou.aliyuncs.com/app%2Fspace%2F78373e955caf4eb8a3a82b36e7d971f6_img-1.png",
            type: "point",
            visible: true,
            columns: [],
            properties: {
              list: {
                label: "建（构）筑物信息采集表",
                columns: [
                  {
                    label: "编号",
                    prop: "buildinforcollnum",
                    type: "input",
                    visible: true,
                  },
                ],
              },
            },
            style: {},
            features: [],
          },
        ],
      },
      drawMode: "",
      selectLayer: "",
      selectFeature: "",
      configType: "",
      editMode: false,
      isLoad: false,
      selectPlottingName: "",
      selectPlottingIcon: "",
    };
  },
  getters: {},
  actions: {
    clearDataset() {
      this.datasetInfo = {
        id: 1,
        createTime: "2023-12-19 19:46:53",
        updateTime: "2023-12-19 19:46:53",
        pid: "3cc18de0-9e64-11ee-b7f5-07df68be1add",
        name: "默认数据集",
        img: "https://rcs-bucket.oss-cn-hangzhou.aliyuncs.com/app%2Fspace%2F4b2eac181cfb4c6a80c3110cf77e82f1_img-2.png",
        location: "POINT (116.32 39.89)",
        scale: 9,
        status: 1,
        style: "",
        layers: [
          {
            id: 1,
            createTime: "2023-12-19 19:52:03",
            updateTime: "2024-01-04 09:41:38",
            name: "",
            pid: "f4830710-9e64-11ee-adb5-3b52745da4bb",
            img: "https://rcs-bucket.oss-cn-hangzhou.aliyuncs.com/app%2Fspace%2F78373e955caf4eb8a3a82b36e7d971f6_img-1.png",
            type: "point",
            visible: true,
            columns: [],
            properties: {
              list: {
                label: "建（构）筑物信息采集表",
                columns: [
                  {
                    label: "编号",
                    prop: "buildinforcollnum",
                    type: "input",
                    visible: true,
                  },
                ],
              },
            },
            style: {},
            features: [],
          },
        ],
      };
      this.drawMode = "";
      (this.selectLayer = ""), (this.selectFeature = "");
      this.configType = "";
      this.editMode = false;
      this.isLoad = false;
      this.selectPlottingName = "";
      this.selectPlottingIcon = "";
    },
    async getDatasetInfo(id: string) {
      const datasetInfo: any = await getDataset(id);
      this.datasetInfo = datasetInfo.data;
      this.configType = "layer";
      if (!this.datasetInfo.location) {
        this.datasetInfo.location = WKT.parse("POINT (116.32 39.89)");
        // this.datasetInfo.location = WKT.parse(this.datasetInfo.location);
      } else {
        // this.datasetInfo.location = WKT.parse("POINT (116.32 39.89)");
      }
      // if (this.datasetInfo.location) {
      //   this.datasetInfo.location = WKT.parse(this.datasetInfo.location);
      // } else {
      //   this.datasetInfo.location = WKT.parse("POINT (116.32 39.89)");
      // }
      this.datasetInfo.layers.forEach((layer) => {
        // layer.features.forEach((feature) => {
        //   feature.geometry = WKT.parse(feature.geometry);
        //   if (feature.center) {
        //     feature.center = WKT.parse(feature.center);
        //   }
        // });
        if (!layer.columns) {
          layer.columns = [];
        }
      });
      // console.log(this.datasetInfo)
      if (this.datasetInfo.layers.length > 0) {
        this.selectLayer = this.datasetInfo.layers[0].pid;
        // alert(this.selectLayer)
      }
      return this.datasetInfo as IDataset;
    },
    async getOpenDatasetInfo(id: string) {
      const datasetInfo: any = await getOpenDataset(id);
      this.datasetInfo = datasetInfo.data;
      this.configType = "layer";
      if (!this.datasetInfo.location) {
        this.datasetInfo.location = WKT.parse("POINT (116.32 39.89)");
        // this.datasetInfo.location = WKT.parse(this.datasetInfo.location);
      } else {
        // this.datasetInfo.location = WKT.parse("POINT (116.32 39.89)");
      }
      // this.datasetInfo.layers.forEach((layer) => {
      //   layer.features.forEach((feature) => {
      //     feature.geometry = WKT.parse(feature.geometry);
      //     feature.center = WKT.parse(feature.center);
      //   });
      // });
      // console.log(this.datasetInfo)
      if (this.datasetInfo.layers.length > 0) {
        this.selectLayer = this.datasetInfo.layers[0].pid;
        // alert(this.selectLayer)
      }
      return this.datasetInfo;
    },
    async getFeaturesByPid(pid: string) {
      const layer = this.datasetInfo.layers.find(
        (item: { pid: string }) => item.pid == pid
      );
      if (layer) {
        const res: any = await getFeatures({
          layer: layer.id,
          page: 1,
          size: 200,
        });

        return res.data;
      } else {
        new Error(`图层PID:${pid} 不存在`);
      }
    },
    getLayerByPid(pid: string) {
      const layer = this.datasetInfo.layers.find(
        (item: { pid: string }) => item.pid == pid
      );
      if (layer) {
        const res = JSON.parse(JSON.stringify(layer));
        delete res.features;
        return res;
      }
    },
    getOriginalLayerByPid(pid: string) {
      const layer = this.datasetInfo.layers.find(
        (item: { pid: string }) => item.pid == pid
      );
      return layer;
    },
    getCurrentLayer() {
      const layer = this.datasetInfo.layers.find(
        (item: { pid: string }) => item.pid == this.selectLayer
      );
      return layer as ILayer;
    },
    getCurrentFeatures() {
      const layer = this.datasetInfo.layers.find(
        (item: { pid: string }) => item.pid == this.selectLayer
      );
      if (layer) {
        return layer.features;
      } else {
        return [];
      }
    },
    getCurrentFeaturesByGeoJson() {
      const layer = this.datasetInfo.layers.find(
        (item: { pid: string }) => item.pid == this.selectLayer
      );
      return {
        type: "geojson",
        data: {
          type: "FeatureCollection",
          features: layer.features?.map((item: any) => {
            item.properties["pid"] = item.pid;
            item.properties["name"] = item.name;
            item.properties["img"] = item.img;
            return {
              type: "Feature",
              geometry: item.geometry,
              properties: item.properties,
            };
          }),
        },
        properties: layer.properties,
      };
    },
  },
});
